//
//  GMLookWuliuViewController.m
//  GuestMore
//
//  Created by apple on 2020/8/19.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THLookWuLiuViewController.h"
#import "GMMineWuLiuModel.h"
@interface THLookWuLiuViewController ()
@property(nonatomic, strong) GMMineWuLiuModel                     *detail_model;/**<  */
@end

@implementation THLookWuLiuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"物流详情";
    [self send_wuliu_detail_request];
    // Do any additional setup after loading the view.
}
#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    UIView * header = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    
    CGFloat mine_top = 20;
    
    ({
        UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(16, mine_top, UISCREEN_WIDTH - 32, 70)];
        whiteView.layer.cornerRadius = 10;
        whiteView.clipsToBounds = YES;
        [header addSubview:whiteView];
        whiteView.backgroundColor = UIColor.whiteColor;
        mine_top = whiteView.bottom + 20;
        
        UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12, 14, 40, 40)];
        stoneobs_ui_imageView.layer.cornerRadius = 20;
        [whiteView addSubview:stoneobs_ui_imageView];
        [stoneobs_ui_imageView sd_setImageWithURL:self.detail_model.goods_img.mj_url];
        
        ({
            NSString * title = [NSString stringWithFormat:@"%@",self.detail_model.kd];
            UIView * view = [self copy_view_form_left_title:@"物流公司" right_title:title show:0];
            view.top = 15;
            view.left = 69;
            [whiteView addSubview:view];
        });
        ({
            NSString * title = [NSString stringWithFormat:@"%@",self.detail_model.LogisticCode];
            UIView * view = [self copy_view_form_left_title:@"快递单号" right_title:title show:1];
            view.top = 42;
            view.left = 69;
            [whiteView addSubview:view];
        });
    });
    
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(16, mine_top, UISCREEN_WIDTH - 32, 70)];
    whiteView.layer.cornerRadius = 10;
    whiteView.clipsToBounds = YES;
    [header addSubview:whiteView];
    whiteView.backgroundColor = UIColor.whiteColor;
    mine_top = whiteView.bottom + 20;
    
    
    CGFloat beginY ;
    CGFloat endY ;
    CGFloat top = 20;
    for (NSInteger i = 0 ; i < self.detail_model.Traces.count; i ++) {
        GMMineWuLiuModelItemModel * model = self.detail_model.Traces[i];
        
        ({
            STButton * button = [[STButton alloc] initWithFrame:CGRectMake(20-5, top, 10, 10)
                                                          title:nil
                                                     titleColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                      titleFont:15
                                                   cornerRadius:5
                                                backgroundColor:WH_UICOLOR_FROM_RGBA(0xB9BBCA)
                                                backgroundImage:nil
                                                          image:nil];
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
            [whiteView addSubview:button];
            if (i==0) {
                button.backgroundColor = WH_UICOLOR_FROM_RGBA(0xFF5A00);
                beginY = button.centerY;
            }
            if (i==self.detail_model.Traces.count-1) {
                endY = button.centerY;
            }
            top = button.bottom;
            
                                    
            ({
                UIView * subButton = [self wuliu_view_form_left_title:model.AcceptStation right_title:model.AcceptTime show:!i];
                subButton.top = top;
                top = subButton.bottom;
                [whiteView addSubview:subButton];
            });

            top = top + 15;
        });
    }
    
    whiteView.height = top + 20;
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(20, beginY, 1, endY - beginY)];
    line.backgroundColor = WH_UICOLOR_FROM_RGBA(0xD9D9D9);
    [whiteView insertSubview:line atIndex:0];
    header.height = whiteView.bottom + 20;
    self.tableView.tableHeaderView = header;
    
}
- (STButton*)buttonFormTitle:(NSString*)title color:(UIColor*)color{
    STButton * button = [[STButton alloc] initWithFrame:CGRectMake(48, 0, UISCREEN_WIDTH - 96, 25)
                                                  title:title
                                             titleColor:color
                                              titleFont:11
                                           cornerRadius:0
                                        backgroundColor:nil
                                        backgroundImage:nil
                                                  image:nil];
    button.titleLabel.numberOfLines = 0;
    CGFloat height = [title stoneobs_fun_st_heigthWithwidth:button.width font:11];
    button.height = height + 10;
    return button;
}
- (UIView*)copy_view_form_left_title:(NSString*)left_title right_title:(NSString*)right_title show:(BOOL)show{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH - 32 - 49 - 15, 20)];
    
    STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(16, 0, view.width - 32 - 40, view.height)
                                                            text:[NSString stringWithFormat:@"%@:   %@",left_title,right_title]
                                                       textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                            font:12
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentLeft];
    [view addSubview:stoneobs_ui_label];
    [NSString stoneobs_fun_lableAutoAdjustheightWithLabel:stoneobs_ui_label];
    if (stoneobs_ui_label.height < 20) {
        stoneobs_ui_label.height = 20;
    }
    view.height = stoneobs_ui_label.bottom;
    stoneobs_ui_label.attributedText = [stoneobs_ui_label.text stoneobs_fun_st_convertAttributeStringWithKeyWord:right_title attributes:@{NSForegroundColorAttributeName:STONEOBS_UI_THIRD_TEXT_COLOR}];
    
    
    STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)
                                                              title:nil
                                                         titleColor:nil
                                                          titleFont:0
                                                       cornerRadius:0
                                                    backgroundColor:nil
                                                    backgroundImage:nil
                                                              image:ST_IMAGE_NAMED(@"复制")];
    [stoneobs_ui_button setClicAction:^(UIButton *sender) {
        [SVProgressHUD showSuccessWithStatus:@"复制成功"];
        [UIPasteboard.generalPasteboard setString:right_title];
    }];
    [view addSubview:stoneobs_ui_button];
    stoneobs_ui_button.right = view.width - 10;
    stoneobs_ui_button.centerY = view.height / 2;
    stoneobs_ui_button.hidden = !show;
    return view;
}
- (UIView*)wuliu_view_form_left_title:(NSString*)left_title right_title:(NSString*)right_title show:(BOOL)show{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(65, 0, UISCREEN_WIDTH - 32 - 65 - 15, 40)];
    
    STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(16, 0, view.width - 32 - 40, view.height)
                                                            text:[NSString stringWithFormat:@"%@",left_title]
                                                       textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                            font:12
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentLeft];
    [view addSubview:stoneobs_ui_label];
    [NSString stoneobs_fun_lableAutoAdjustheightWithLabel:stoneobs_ui_label];
    if (stoneobs_ui_label.height < 20) {
        stoneobs_ui_label.height = 20;
    }
    
    STLabel * stoneobs_ui_s_label = [[STLabel alloc] initWithFrame:CGRectMake(16, stoneobs_ui_label.bottom + 8, view.width - 32 - 40, view.height)
                                                            text:[NSString stringWithFormat:@"%@",right_title]
                                                       textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                            font:11
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentLeft];
    [view addSubview:stoneobs_ui_s_label];
    [NSString stoneobs_fun_lableAutoAdjustheightWithLabel:stoneobs_ui_s_label];
    if (stoneobs_ui_s_label.height < 20) {
        stoneobs_ui_s_label.height = 20;
    }
    view.height = stoneobs_ui_s_label.bottom ;


    if (show) {
        stoneobs_ui_label.textColor = WH_UICOLOR_FROM_RGBA(0xFD5325);
    }
    return view;
}
- (void)send_wuliu_detail_request{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    if (self.oid.length) {
        [stoneobs_net_param_dic setObject:self.oid forKey:@"id"];
    }
    
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/orders/express"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumGet
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
        GMMineWuLiuModel * model = [GMMineWuLiuModel mj_objectWithKeyValues:stoneobs_net_model_dic];
        self.detail_model = model;
        [self stoneobs_ui_configSubView];
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}


@end
